package token

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

type TokenController struct {
	log     zap.Logger
	service TokenService
}

func NewTokenController(service TokenService, log zap.Logger) TokenController {
	return TokenController{service: service, log: log}
}

func (s TokenController) GetToken(ctx *gin.Context) {
	body, err := ctx.GetRawData()
	if err != nil {
		ctx.JSON(http.StatusBadRequest, nil)
		return
	}
	var token Token
	_ = json.Unmarshal(body, &token)
	resp := s.service.GetToken(token)
	ctx.JSON(http.StatusOK, resp)
	return
}

func (s TokenController) Login(ctx *gin.Context) {
	body, err := ctx.GetRawData()
	if err != nil {
		ctx.JSON(http.StatusBadRequest, nil)
		return
	}
	var token User
	_ = json.Unmarshal(body, &token)

	ctx.JSON(http.StatusOK, token)
	return
}
